package com.hntx.joymusic;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.hntx.joymusic.microphone.InterfaceMicInOutAction;
import com.hntx.joymusic.microphone.ReceiverConst;

public class HomeKeyReceiver extends BroadcastReceiver {

//    static final String TAG = "HomeKeyReceiver";
//    final String SYSTEM_DIALOG_REASON_KEY = "reason";
//    final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";
//
//    @Override
//    public void onReceive(Context context, Intent intent) {
//        String action = intent.getAction();
//        if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
//            String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
//            if (SYSTEM_DIALOG_REASON_HOME_KEY.equals(reason)) {
//                Log.d(TAG, "HOME_KEY_PRESSED");
//                MyApp.getInstance().exitApp();
//            }
//        }
//    }

    static final String TAG = "HomeKeyReceiver";
    final String SYSTEM_DIALOG_REASON_KEY = "reason";
    final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";
    private static InterfaceMicInOutAction mMicInOutAction;

    //设置麦克风插入拔出监听
    public static void setMicInOutAction(InterfaceMicInOutAction micInOutAction){
        mMicInOutAction = micInOutAction;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
            String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
            if (SYSTEM_DIALOG_REASON_HOME_KEY.equals(reason)) {
                Log.d(TAG, "HOME_KEY_PRESSED");
                MyApp.getInstance().exitApp();
            }
        }
        if (ReceiverConst.ACTION_MIC_IN.equals(intent.getAction())){
            if (mMicInOutAction != null) {
                mMicInOutAction.micIn();
            }
        }
        if (ReceiverConst.ACTION_MIC_OUT.equals(intent.getAction())){
            if (mMicInOutAction != null) {
                mMicInOutAction.micOut();
            }
        }
        if (ReceiverConst.ACTION_EXIT.equals(intent.getAction())) {
//            try {
//                sendPlayerExitLog();
//                loginOut(context);
//            }catch (Exception e){
//                e.printStackTrace();
//            }finally{
//                ProcessPreferences.getInstance(context).clear();
//                android.os.Process.killProcess(android.os.Process.myPid());
//                System.exit(0);
//            }
        }
    }

}